package com.hemerasolutions.v2trainer.model.exerciseGroup;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

import com.hemerasolutions.v2trainer.model.base.BaseModel;
import com.hemerasolutions.v2trainer.model.interfaces.BaseModelInterface;
import com.hemerasolutions.v2trainer.model.library.Library;

@Entity
public class VersionableExerciseGroup extends BaseModel implements BaseModelInterface {

	private static final long serialVersionUID = 1L;

    @OneToMany(mappedBy="versionableExerciseGroup",cascade = CascadeType.ALL, fetch=FetchType.LAZY) 
    private List<ExerciseGroup> exerciseGroupVersions;

    @ManyToOne
    private Library library;

    @Transient 
    private ExerciseGroup exerciseGroup;
    
    public VersionableExerciseGroup() {
		super();
		this.exerciseGroupVersions = new ArrayList<ExerciseGroup>();
    }

	public VersionableExerciseGroup(VersionableExerciseGroup other, Library library){
		this();
		this.library = library;
		
		for(ExerciseGroup exerciseGroup: other.exerciseGroupVersions){
			
			ExerciseGroup aux = new ExerciseGroup(exerciseGroup,this);
			
			this.exerciseGroupVersions.add(aux);

		}
	}

	public ExerciseGroup addVersion(){

		int version = 1;
		
		for(ExerciseGroup eg:this.exerciseGroupVersions){
			if(eg.getVersion() > version){
				version = eg.getVersion() + 1;
			}
		}

		ExerciseGroup eg = new ExerciseGroup();
		eg.setVersionableExerciseGroup(this);
		eg.setVersion(version);

		
		this.exerciseGroupVersions.add(eg);

		return eg;
		
	}
	
	public List<ExerciseGroup> getExerciseGroupVersions() {
		return exerciseGroupVersions;
	}

	public void setExerciseGroupVersions(List<ExerciseGroup> exerciseGroupVersions) {
		this.exerciseGroupVersions = exerciseGroupVersions;
	}

	public ExerciseGroup getCurrentVersion() {
		ExerciseGroup currentVersion = null;
		int version = 0;
		
		for(ExerciseGroup exerciseGroup:this.exerciseGroupVersions){
			if(version < exerciseGroup.getVersion()){
				currentVersion = exerciseGroup;
				version = currentVersion.getVersion();
			}
		}
		return currentVersion;
	}

	public void setCurrentVersion(ExerciseGroup currentVersion) {

		for(int x = 0; x < this.getExerciseGroupVersions().size(); x++){
			ExerciseGroup eg = this.getExerciseGroupVersions().get(x);
			if(eg.getUuid() ==  currentVersion.getUuid()){
				this.getExerciseGroupVersions().set(x,currentVersion);
			}
		} 
	}

	
	
	public ExerciseGroup getExerciseGroup() {
		if(this.exerciseGroup == null){
			this.exerciseGroup = this.getCurrentVersion();
		}
		for(ExerciseGroup version:this.exerciseGroupVersions){
			if(version.getUuid() == this.exerciseGroup.getUuid()){
				this.exerciseGroup = version;
			}
		}
		return this.exerciseGroup;

	}

	public void setExerciseGroup(ExerciseGroup mod) {
		for(int x = 0; x < this.getExerciseGroupVersions().size(); x++){
			ExerciseGroup version = this.getExerciseGroupVersions().get(x);
			if(version.getUuid() ==  mod.getUuid()){
				this.getExerciseGroupVersions().set(x,mod);
				this.exerciseGroup = version;
			}
		} 
	}	

	public Library getLibrary() {
		return library;
	}

	public void setLibrary(Library library) {
		this.library = library;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((uuid == null) ? 0 : uuid.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (!super.equals(obj)) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		VersionableExerciseGroup other = (VersionableExerciseGroup) obj;
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}
		if (uuid == null) {
			if (other.uuid != null) {
				return false;
			}
		} else if (!uuid.equals(other.uuid)) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "VersionableExerciseGroup [id=" + id + ", uuid=" + uuid + "]";
	}

	
	
}
